package com.lzh.chatnote.user.domain.entity;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;
import java.util.Optional;

@Data
public class IpInfo implements Serializable {

    private String createIp;

    private IpInfoDetail createIpInfoDetail;

    private String updateIp;

    private IpInfoDetail updateIpInfoDetail;

    public void reflushIp(String ip){
        if (StrUtil.isBlank(ip)){
            return;
        }
        if(StrUtil.isBlank(this.createIp)){
            this.createIp  = ip;
        }
        this.updateIp = ip;
    }


    public void reflushIpDetail(IpInfoDetail ipInfoDetail){
        if (Objects.equals(createIp, ipInfoDetail.getIp())){
            this.createIpInfoDetail = ipInfoDetail;
        }

        if (Objects.equals(updateIp, ipInfoDetail.getIp())){
            this.updateIpInfoDetail = ipInfoDetail;
        }
    }

    public String tryGetReflushIp(IpInfo ipInfo) {
        boolean eq = Optional.ofNullable(ipInfo.getUpdateIpInfoDetail())
                .map(IpInfoDetail::getIp)
                .filter(ip -> !Objects.equals(ip, this.updateIp))
                .isPresent();
        //相同 则不刷新 ，不同则刷新
        return eq ?  null : ipInfo.getUpdateIp();
    }
}
